import org.jhotdraw.framework.*;
import org.jhotdraw.samples.net.NodeFigure;
import org.jhotdraw.standard.*;
import org.jhotdraw.figures.*;
import org.jhotdraw.util.*;

import java.util.*;
import java.util.List;
import java.awt.*;

/**
 * @version <$CURRENT_VERSION$>
 */
public class StateFigure extends NodeFigure {
	private static final int BORDER = 100;
	private List fConnectors;
	private boolean fConnectorsVisible;
	protected Color shape_color = Color.RED;
	private boolean isStartState = false, isAcceptState = false;

	public StateFigure() {
		initialize();
		fConnectors = null;
	}

	@Override
	protected void markDirty() {

	}

	public StateFigure(int x, int y, String name) {
		this.basicDisplayBox(new Point(x, y), new Point(x + 40, y + 40));
		initialize();
		fConnectors = null;
		this.setText(name);
	}

	public Rectangle displayBox() {
		Rectangle box = super.displayBox();
		// box.grow(-box.width,-box.height);
		box.setSize(BORDER, BORDER);
		return box;
	}

	public boolean containsPoint(int x, int y) {
		// add slop for connectors
		if (fConnectorsVisible) {
			Rectangle r = displayBox();
			int d = LocatorConnector.SIZE / 2;
			r.grow(d, d);
			return r.contains(x, y);
		}
		return super.containsPoint(x, y);
	}

	private void drawBorder(Graphics g) {
		Rectangle r = displayBox();
		g.setColor(Color.WHITE);
		g.drawRect(r.x, r.y, r.width - 1, r.height - 1);
		g.setColor(Color.BLACK);
		g.drawOval(r.x, r.y, 100, 100);
		
		if (isAcceptState == true) {
			g.drawOval(5 + r.x, 5 + r.y, 90, 90);
		}
		if (isStartState == true) {
			g.drawLine(r.x - 5, r.y + 50, r.x + 20, r.y + 50);
			g.drawString("START", r.x + 18, (r.y) + 55);
		}
	}

	@Override
	public void draw(Graphics g) {
		super.draw(g);
		drawBorder(g);
		drawConnectors(g);
	}

	private void drawConnectors(Graphics g) {
		if (fConnectorsVisible) {
			Iterator iter = connectors();
			while (iter.hasNext()) {
				((Connector) iter.next()).draw(g);
			}
		}
	}

	public void connectorVisibility(boolean isVisible,
			ConnectionFigure courtingConnection) {
		fConnectorsVisible = isVisible;
		invalidate();
	}

	/**
	 */
	public Connector connectorAt(int x, int y) {
		return findConnector(x, y);
	}

	/**
	 */
	public Iterator connectors() {
		if (fConnectors == null) {
			createConnectors();
		}
		return fConnectors.iterator();
	}

	private void createConnectors() {
		fConnectors = CollectionsFactory.current().createList(8);
		fConnectors.add(new LocatorConnector(this, RelativeLocator.north()));
		fConnectors.add(new LocatorConnector(this, RelativeLocator.south()));
		fConnectors.add(new LocatorConnector(this, RelativeLocator.west()));
		fConnectors.add(new LocatorConnector(this, RelativeLocator.east()));
	}

	private Connector findConnector(int x, int y) {
		// return closest connector
		long min = Long.MAX_VALUE;
		Connector closest = null;
		Iterator iter = connectors();
		while (iter.hasNext()) {
			Connector c = (Connector) iter.next();
			Point p2 = Geom.center(c.displayBox());
			long d = Geom.length2(x, y, p2.x, p2.y);
			if (d < min) {
				min = d;
				closest = c;
			}
		}
		return closest;
	}

	private void initialize() {
		setText("s0");
		Font fb = new Font("Helvetica", Font.BOLD, 20);
		setFont(fb);
		createConnectors();
	}

	public Figure getRepresentingFigure() {
		return this;
	}

	public void createState() {
		State s = new State(this.getText());
		s.setName(this.getText());
		s.setAccepting(false);

	}

	public void setIsAcceptState(Boolean b) {
		isAcceptState = b;
	}

	public Boolean getIsAcceptState(Boolean b) {
		isAcceptState = b;
		return isAcceptState;
	}

	public void setIsStartState(Boolean b) {
		isStartState = b;
	}

	public Boolean getIsStartState(Boolean b) {
		isStartState = b;
		return isStartState;
	}
}
